package com.yupi.yuojcodesandbox.judge.codesandbox;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.UUID;
import com.yupi.yuojcodesandbox.judge.codesandbox.model.ExecuteCodeRequest;
import com.yupi.yuojcodesandbox.judge.codesandbox.model.ExecuteCodeResponse;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:Dong
 * @Date: 2024/4/21  9:19
 * @Version 1.0
 */
public class CodeNavicateSandbox implements CodeSandbox {
    private static final String GLOBAL_CODE_DIR_NAME = "tempDir";

    private static final String GLOBAL_CODE_NAME = "SimpleCompute.java";

    public static void main(String[] args) throws IOException {
        CodeNavicateSandbox codeNavicateSandbox = new CodeNavicateSandbox();
        ExecuteCodeRequest executeCodeRequest = new ExecuteCodeRequest();
        executeCodeRequest.setInputList(Arrays.asList("1 2","1 3"));
        String code = ResourceUtil.readStr("D:\\dysJavaProject\\yuoj-code-sandbox\\tempCode\\b0de787b-8f99-4f30-bbfc-9c6c017217b5\\SimpleCompute.java", StandardCharsets.UTF_8);
        executeCodeRequest.setCode(code);
        executeCodeRequest.setLanguage("java");
        ExecuteCodeResponse executeCodeResponse = codeNavicateSandbox.executeCode(executeCodeRequest);
        System.out.println("executeCodeResponse = " + executeCodeResponse);

    }

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) throws IOException {
     List<String> inputList = executeCodeRequest.getInputList();
     String code = executeCodeRequest.getCode();
     String language = executeCodeRequest.getLanguage();

     //1.把用户代码保存为文件
        String property = System.getProperty("user.dir");
        String globalCodePathName = property+File.separator+GLOBAL_CODE_DIR_NAME;
        if (!FileUtil.exist(globalCodePathName)){
            //不存在则创建一个新的目录
            FileUtil.mkdir(globalCodePathName);
        }
        //把用户代码隔离存放,每个用户的代码放在不同的路径下
        String userCodeParentPath = globalCodePathName + File.separator + UUID.randomUUID();
        String userCodePath = userCodeParentPath + File.separator + GLOBAL_CODE_NAME;
        //将代码内容写入文件
        File file = FileUtil.writeString(code, userCodePath, StandardCharsets.UTF_8);

        //编译代码
        String format = String.format("javac -encoding utf-8 %s", file.getAbsolutePath());
        try {
            Process processCompile = Runtime.getRuntime().exec(format);
            int exitValue = processCompile.waitFor();
            if (exitValue == 0){
                System.out.println("\"执行成功\" = " + "执行成功");
                //获取缓冲区内容
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(processCompile.getInputStream()));
                String compileBufferLine;
                while((compileBufferLine = bufferedReader.readLine())!= null){
                    System.out.println("compileBufferLine = " + compileBufferLine);
                }
            }else{
                System.out.println("执行失败，错误码是"+exitValue);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }
}
